package com.lotter.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lotter.api.service.CashRecordService;
import com.lotter.api.service.HousernewsService;
import com.lotter.api.service.TouristService;
import com.lotter.api.service.UserAccountService;
import com.lotter.common.enumobject.UserAccountTypeEunm;
import com.lotter.common.util.StringUtil;
import com.lotter.model.hoursenew.HouserNews;
import com.lotter.model.user.UserAccount;

/**
 * 游客访问
 * @author zhangzechi
 */
@Controller
public class TouristContriller extends BaseController {

	@Autowired
	private UserAccountService userAccountService ;

	@Autowired
	private TouristService touristService;
	
	@Autowired
	private HousernewsService housernewsService;
	
	@Autowired
	private CashRecordService cashRecordService;
	
	/**
	 * 房间列表
	 * @param hourselevel
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/shenlist",method = RequestMethod.GET)
	public Map<String,Object> shenList(){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			userAccountService.findUserAccountList(result);
		} catch (Exception e) {
			logger.error("房主列表查询失败，请检查代码");
			return ajaxLotterServer("服务器异常,请联系管理员");
		}
		return ajaxLotterSuccess(result);
	}
	
	/**
	 * 前端用户新闻列表
	 * @param uid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/newsList",method= {RequestMethod.GET,RequestMethod.POST})
	public  Map<String,Object> newsList(@RequestParam(value="uid" ,required = true) Long uid,
			@RequestParam(value ="numPerPage",required = true) Integer numPerPage,
			@RequestParam(value ="pageNum",required = true) Integer pageNum){
		 if(uid == null){
			 return ajaxLotterStage("房主id 没有收到");
		 }
		 Map<String,Object> result = new HashMap<String, Object>();
		 try {
			 UserAccount userAccount =userAccountService.selectByPrimaryKey(uid);
			 result.put("userAccount", userAccount);
			 List<HouserNews> list =housernewsService.findNewsList(uid, numPerPage, pageNum);
			 result.put("data", list);
		  } catch (Exception e) {
			logger.error("房间详情查询失败，请检查代码");
			return ajaxLotterServer("服务器异常,请联系管理员");
		  }
		 return ajaxLotterSuccess(result) ;
	}
	
	/**
	 * 房间详情
	 * @param request
	 * @param uid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/houserDetails",method= RequestMethod.GET)
	public Map<String,Object> houserDetails(HttpServletRequest request,
			@RequestParam(value="uid" ,required = true) Long uid) {
		 if(uid == null){
			 return ajaxLotterStage("房主id 没有收到");
		 }
		 Map<String,Object> result = new HashMap<String, Object>();
		 try {
			 UserAccount userAccount =userAccountService.selectByPrimaryKey(uid);
			 if(userAccount ==null || userAccount.getUtype() 
					 ==UserAccountTypeEunm.SUBSCRIBER.getCode()){
				 return ajaxLotterUser("房间信息有误");
			 }
			 HouserNews houserNews = housernewsService.findHousernewsDto(uid);
			 if(houserNews ==null){
				 return ajaxLotterUser("房主还没有任何新闻信息");
			 }
			 result.put("houserNews", houserNews);
			 touristService.houserDetails(result, uid,houserNews.getId());
		  } catch (Exception e) {
			logger.error("房间详情查询失败，请检查代码");
			return ajaxLotterServer("服务器异常,请联系管理员");
		  }
		 return ajaxLotterSuccess(result) ;
	}
	
	/**
	 * 比分详情
	 * @param request
	 * @param uid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/scoreDetails",method= {RequestMethod.GET,RequestMethod.POST})
	public Map<String,Object> scoreDetails(HttpServletRequest request,
			@RequestParam(value="phase" ,required = true) String phase) {
		 if(StringUtil.isBlank(phase)){
			 return ajaxLotterStage("彩票期数没有收到");
		 }
		 Map<String,Object> result = new HashMap<String, Object>();
		 cashRecordService.scopeDetail(result, phase);
		 return ajaxLotterSuccess(result);
	}
	

	
}
